package com.demostic.demostic_test.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Matrix;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.PayloadCallback;
import com.accloud.service.ACException;
import com.accloud.service.ACUserDevice;
import com.demostic.demostic_test.R;
import com.demostic.demostic_test.base.BaseActivity;
import com.demostic.demostic_test.fragment.DeviceFragment;
import com.demostic.demostic_test.fragment.PersonalFragment;
import com.demostic.demostic_test.logUtils.LogcatHelper;
import com.demostic.demostic_test.utils.MyLog;
import com.demostic.demostic_test.utils.ToastUtils;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import io.reactivex.functions.Consumer;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    public static final String DEV_FRAGMENT_TAG = "DeviceFragment";
    public static final String PER_FRAGMENT_TAG = "PersonalFragment";
    private final String TAG = MainActivity.class.getSimpleName();
    private Context context;
    private int current = -1;
    private DeviceFragment deviceFragment;
    private PersonalFragment personalFragment;
    private long exitTime = 0;
    private int finishTag;
    private FragmentManager fm;
    private ImageView image_add;
    private ImageView image_device;
    private ImageView image_msg;
    private ImageView image_personal;
    private RelativeLayout rl_device;
    private RelativeLayout rl_personal;
    private TextView tv_device;
    private TextView tv_personal;
    private TextView tv_title;

    @Override // com.demostic.demostic_test.base.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        new RxPermissions(this).requestEach("android.permission.WRITE_EXTERNAL_STORAGE").subscribe(new Consumer<Permission>() {
            /* class com.demostic.demostic_test.activity.MainActivity.AnonymousClass1 */

            public void accept(Permission permission) {
                if (!permission.granted) {
                    ToastUtils.showToast(MainActivity.this.context, MainActivity.this.getString(R.string.access_storage));
                } else {
                    LogcatHelper.getInstance(MainActivity.this.context).start();
                }
            }
        }, new Consumer<Throwable>() {
            /* class com.demostic.demostic_test.activity.MainActivity.AnonymousClass2 */

            public void accept(Throwable throwable) throws Exception {
                MyLog.e(MainActivity.this.TAG, "RxPermissions Throwable:" + throwable.getMessage());
            }
        });
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity, android.app.Activity
    public void onResume() {
        super.onResume();
        Matrix matrix = new Matrix();
        float[] values = new float[9];
        matrix.setScale(1.0f, 1.0f);
        matrix.getValues(values);
        float x = (385.0f - values[2]) / values[0];
        MyLog.e(this.TAG, "dsfsfsfsa:" + x + "," + ((590.0f - values[5]) / values[0]) + "," + values[0]);
    }

    private void initFragment() {

        this.deviceFragment = new DeviceFragment();
        this.personalFragment = new PersonalFragment();
        this.fm = getSupportFragmentManager();
        FragmentTransaction ft = this.fm.beginTransaction();
        ft.add(R.id.container, this.deviceFragment, DEV_FRAGMENT_TAG);
        ft.add(R.id.container, this.personalFragment, PER_FRAGMENT_TAG);
        ft.hide(this.personalFragment).show(this.deviceFragment).commit();
//        ft.show(deviceFragment);
    }

    private void initView() {
        this.context = this;
        this.rl_device = (RelativeLayout) findViewById(R.id.rl_device);
        this.rl_personal = (RelativeLayout) findViewById(R.id.rl_personal);
        this.image_device = (ImageView) findViewById(R.id.image_device);
        this.image_personal = (ImageView) findViewById(R.id.image_personal);
        this.image_add = (ImageView) findViewById(R.id.image_add);
        this.image_msg = (ImageView) findViewById(R.id.image_msg);
        this.tv_device = (TextView) findViewById(R.id.tv_device);
        this.tv_personal = (TextView) findViewById(R.id.tv_personal);
        this.tv_title = (TextView) findViewById(R.id.tv_title);
        this.rl_personal.setOnClickListener(this);
        this.rl_device.setOnClickListener(this);
        this.image_add.setOnClickListener(this);
        this.image_msg.setOnClickListener(this);
        this.image_device.setSelected(true);
        this.tv_device.setSelected(true);
        this.current = R.id.rl_device;
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_device /* 2131755282 */:
                transformUi(R.id.rl_device);
                return;
            case R.id.rl_personal /* 2131755285 */:
                transformUi(R.id.rl_personal);
                return;
            case R.id.image_add /* 2131755291 */:
                if (!AC.accountMgr().isLogin()) {
                    startActivity(new Intent(this, LoginActivity.class));
                    return;
                } else {
                    startActivity(new Intent(this, SelectActivity_.class));
                    return;
                }
            case R.id.image_msg /* 2131755292 */:
//                if (AC.accountMgr().isLogin()) {
//                    Intent intent = new Intent(this.context, MsgActivity.class);
//                    intent.putExtra("isFromMain", true);
//                    startActivity(intent);
//                    return;
//                }
                ToastUtils.showToast(this.context, getString(R.string.main_aty_login_first));
                return;
            default:
                return;
        }
    }

    public void transformUi(int res) {
        boolean isDevice;
        boolean z;
        boolean z2 = true;
        int i = 0;
        FragmentTransaction ft = this.fm.beginTransaction();
        if (res != this.current) {
            if (this.current == R.id.rl_device) {
                isDevice = true;
            } else {
                isDevice = false;
            }
            ImageView imageView = this.image_device;
            if (!isDevice) {
                z = true;
            } else {
                z = false;
            }
            imageView.setSelected(z);
            this.image_personal.setSelected(isDevice);
            TextView textView = this.tv_device;
            if (isDevice) {
                z2 = false;
            }
            textView.setSelected(z2);
            this.tv_personal.setSelected(isDevice);
            this.tv_title.setText(!isDevice ? getString(R.string.main_aty_my_dev) : getString(R.string.main_aty_per_center));
            ImageView imageView2 = this.image_add;
            if (isDevice) {
                i = 8;
            }
            imageView2.setVisibility(i);
            this.current = res;
            ft.hide(!isDevice ? this.personalFragment : this.deviceFragment).show(isDevice ? this.personalFragment : this.deviceFragment);
            ft.commit();
        }
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity, android.app.Activity
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            String shareCode = result.getContents();
            if (!TextUtils.isEmpty(shareCode)) {
                bindDevice(shareCode);
                return;
            }
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void bindDevice(String shareCode) {
        AC.bindMgr().bindDeviceWithShareCode(shareCode, new PayloadCallback<ACUserDevice>() {
            /* class com.demostic.demostic_test.activity.MainActivity.AnonymousClass3 */

            public void success(ACUserDevice userDevice) {
                ToastUtils.showToast(MainActivity.this.context, MainActivity.this.getString(R.string.main_aty_bind_suc));
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                ToastUtils.showToast(MainActivity.this.context, MainActivity.this.getString(R.string.main_aty_bind_fail));
            }
        });
    }

    @Override // android.app.Activity, android.view.KeyEvent.Callback
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode != 4 || event.getAction() != 0) {
            return super.onKeyDown(keyCode, event);
        }
        if (System.currentTimeMillis() - this.exitTime > 2000) {
            ToastUtils.showToast(this.context, getString(R.string.main_aty_press_exit));
            this.exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
        return true;
    }

    /* access modifiers changed from: protected */
    @Override // com.demostic.demostic_test.base.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onStop() {
        super.onStop();
        MyLog.e(this.TAG, "onStop");
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onSaveInstanceState(Bundle outState) {
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onDestroy() {
        super.onDestroy();
    }
}
